<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="职工姓名" prop="deptName">
            <el-input
              v-model="queryParams.deptName"
              placeholder="职工名字"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermission="['personal:dept:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
              v-hasPermission="['personal:dept:delete']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermission="['personal:elderly:export']"
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
            >导出</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="deptList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column label="序号" type="index" align="center" />
          <el-table-column prop="deptNo" label="职工编号" align="center" />
          <el-table-column prop="deptName" label="职工姓名" align="center" />
          <el-table-column prop="sex" label="性别" align="center" width="60">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.sex === '0'" >男</el-tag>
              <el-tag v-else-if="scope.row.sex === '1'" type="danger">女</el-tag>
              <el-tag v-else type="warning">未知</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="deptIdentityid" label="身份证号" align="center" show-overflow-tooltip />
          <el-table-column prop="phonenumber" label="手机号码" align="center" />
          <el-table-column prop="address" label="地址" align="center" show-overflow-tooltip />
          <el-table-column prop="birthday" label="职工生日" align="center" />
          <el-table-column prop="entryTime" label="入司时间" align="center" />
          <el-table-column prop="postName" label="岗位" align="center" width="80"/>
          <el-table-column prop="salary" label="薪水" align="center" width="60"/>
          <el-table-column
            label="操作"
            align="center"
            width="200"
            class-name="small-padding fixed-width"
          >
            <template v-if="scope.row.id !== 1" slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermission="['personal:dept:update']"
              >修改</el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermission="['personal:dept:delete']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="职工编号" prop="deptNo">
              <el-input
                v-model="form.deptNo"
                placeholder="请输入职工编号"
                maxlength="32"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职工姓名" prop="deptName">
              <el-input
                v-model="form.deptName"
                placeholder="请输入职工名字"
                maxlength="32"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="性别">
              <el-select v-model="form.sex" placeholder="请选择">
                <el-option :key="'0'" label="男" :value="'0'" />
                <el-option :key="'1'" label="女" :value="'1'" />
                <el-option :key="'2'" label="未知" :value="'2'" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入地址"
                maxlength="124"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="form.phonenumber"
                placeholder="请输入手机号码"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证" prop="deptIdentityid">
              <el-input
                v-model="form.deptIdentityid"
                placeholder="请输入身份证号"
                maxlength="18"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="生日"
              prop="birthday"
            >
              <el-date-picker
                v-model="form.birthday"
                type="date"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="入职时间"
              prop="entryTime"
            >
              <el-date-picker
                v-model="form.entryTime"
                type="date"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="岗位" prop="postId">
              <el-select v-model="form.postId" placeholder="请选择">
                <el-option
                  v-for="post in postList"
                  :key="post.postId"
                  :label="post.postName"
                  :value="post.postId"
                />
                <!-- <el-option :key="'1'" label="女" :value="'1'" /> -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="薪水" prop="salary">
              <el-input
                v-model.number="form.salary"
                placeholder="请输入薪水"
                maxlength="10"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { getToken } from '@/utils/auth'
import {
  addDept,
  delDept,
  exportDept,
  getDept,
  listDept, updateDept
} from '@/api/personal/dept'
import { listPost} from "@/api/personal/post";

export default {
  name: 'Dept',
  data() {
    var checkIdentityId = (rule, value, callback) => {
      const _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      const _IDre15 = /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
      // 校验身份证：
      if (!_IDRe18.test(value) && !_IDre15.test(value)) {
        callback(new Error('身份证号格式不对'))
      } else {
        callback()
      }
    }
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: undefined,
        phonenumber: undefined
      },
      title: '',
      postList: [],
      // 是否显示弹出层
      open: false,
      exportLoading: false,
      // 表单校验,
      rules: {
        deptName: [
          { required: true, message: '用户名称不能为空', trigger: 'blur' },
          {
            min: 2,
            max: 32,
            message: '用户名称长度必须介于 2 和 32 之间',
            trigger: 'blur'
          }
        ],
        deptIdentityid: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          { validator: checkIdentityId, trigger: 'blur' }
        ],
        salary: [
          { type: 'number', message: '薪水必须为数字', trigger: 'blur' },
          { required: true, message: '请填写薪水', trigger: 'blur' }
        ],
        phonenumber: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          },
          {
            pattern: /0?(13|14|15|18|17)[0-9]{9}/,
            message: '手机号格式不对',
            trigger: 'blur'
          }
        ],
        deptNo: [
          {
            required: true,
            message: '请输入员工编号',
            trigger: 'blur'
          }
        ],
        postId: [
          {
            required: true,
            message: '请选择岗位',
            trigger: 'blur'
          }
        ]
      },
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      deptList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {
        id: '',
        deptNo: '',
        deptName: '',
        deptIdentityid: '',
        phonenumber: '',
        address: '',
        sex: '',
        postId: '',
        salary: '',
        entryTime: '',
        birthday: ''
      }
    }
  },
  watch: {},
  created() {
    this.getPostList()
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    getPostList() {
      listPost().then((res) => {
        this.postList = res
      })
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加职工'
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有数据？').then(() => {
        this.exportLoading = true
        return exportDept()
      }).then(response => {
        // this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {})
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listDept(this.queryParams).then((response) => {
        this.deptList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDept(id).then((response) => {
        this.form.id = response.id
        this.form.deptNo = response.deptNo
        this.form.deptName = response.deptName
        this.form.phonenumber = response.phonenumber
        this.form.sex = response.sex
        this.form.address = response.address
        this.form.deptIdentityid = response.deptIdentityid
        this.form.postId = response.postId
        this.form.salary = response.salary
        this.form.entryTime = response.entryTime
        this.form.birthday = response.birthday
        this.open = true
        this.title = '修改职工信息'
      })
    },
    // 表单重置
    reset() {
      this.form = {
        deptName: undefined,
        deptNo: undefined,
        deptIdentityid: undefined,
        phonenumber: undefined,
        address: undefined,
        sex: undefined,
        postId: undefined,
        entryTime: undefined,
        birthday: undefined,
        salary: undefined
      }
      this.resetForm('form')
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除用户编号为"' + ids + '"的数据项？')
        .then(function() {
          return delDept(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid, error) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateDept(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDept(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        } else {
          return
        }
      })
    }
  }
}
</script>
